<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$return['status']=$this->order_status;
$return['send_status']=$this->send_status;
$return['status2']=$this->order_status;
$return['buytype']=$this->buytype;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$shopset=$this->plugin_setting('shopset');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}elseif($_RQ['status']==1){
			$params['status']=array(1,21);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['starttime'] || $_RQ['endtime']){
		if($_RQ['starttime']){
			$return['params']['starttime']=$_RQ['starttime'];
		}
		if($_RQ['endtime']){
			$return['params']['endtime']=$_RQ['endtime'];
		}
		$_RQ['starttime']=$_RQ['starttime']?strtotime($_RQ['starttime']):0;
		$_RQ['endtime']=$_RQ['endtime']?strtotime($_RQ['endtime']):time();
		$params['createtime between']=array($_RQ['starttime'],$_RQ['endtime']);
	}
	$tip='所有配置单显示，列表空调只显示第一个！';
	$tip .='</br><red>【注意】：本场景已开启独立空调展示，只显示本场景下的配置单！</red>';
	$tip .='</br>【<red>配置单时间显示不准确？</red>】：可能服务器的时间不准确，联系管理员同步一下服务器网络时间！';
	$tip .='</br>【<red>导出的配置单是按下单时间？</red>】：因为有些配置单未支付，不存在支付时间，只能按下单时间来导出！';
	$params['plugin_name']='jy_kongtiaoshop';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
		$item['member']['leveltitle']=pdo_getcolumn('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']),'title');
		$item['member']['leveltitle']=$item['member']['leveltitle']?:'普通会员';
		$goods=pdo_get('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		if(!$item['merchid'] && $item['storeid']){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		}
		if($item['merchid']){
			$item['storetitle']='商户';
		}
		$item['storetitle']=$item['storetitle']?:'平台自营';
		
		//获取地址
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['name'] || $item['address']['mobile'])){	
			$address=$item['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
		}
		$item['address']=model('core/member')->get_address_str($address);
		if(($item['sendtype']!=1 && $address['name']) || !$item['realname']){
			$item['realname']=$address['name'];
		}
		if($item['sendtype']==1 && $item['is_daifa']==1){
			$item['sendtype']='daifa';
		}
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['can_refund']=model('jy_weishop/order/refund')->check_order_rerefund($item['id']);
		$item['api_data']=json_decode($item['api_data'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'配置单管理_Order list'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'配置单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'realname','title'=>'联系人_Realname','type'=>'string'),
			array('type'=>'img_string','title'=>'空调名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('type'=>'string_more','title'=>'金额/时间_Price',
			'data'=>array(
				array('title'=>'金额：_Price：','name'=>'price'),
				array('title'=>'时间：_Time：','name'=>'createtime')
				)
			),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'是否支付_Is pay?','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'支付方式_Pay type','name'=>'paytype','type'=>'radio','options'=>'paytype'),
				)
			),
			array('name'=>'status','title'=>'配置单状态_status','type'=>'radio','options'=>'status')
		),
		'action'=>array(
			array('type'=>'href','action'=>'/store.order.add/post/','title'=>'修改配置单','show'=>array('status'=>array(0,4,-1))),
			array('type'=>'href','action'=>'/store.order.dayin/','title'=>'默认配置单'),
			array('type'=>'href','action'=>'/store.order.dayin/tpl01/','title'=>'配置单01'),
		),'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'配置单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>9,'title'=>'配置单状态_Status','type'=>'radio','options'=>'status'),
			array('type'=>'choose','width'=>11,'title'=>'所属会员ID','name'=>'mid','action'=>'core/member.get_member/'),
		)
	);
	$htmls['search'][]=array('name'=>'starttime','width'=>10,'title'=>'开始时间','type'=>'time');
	$htmls['search'][]=array('name'=>'endtime','width'=>10,'title'=>'结束时间','type'=>'time');
	$merchs[0]=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	foreach($merchs as &$mv){
		if($mv['id']){
			$mv['title']='['.$mv['id'].'] '.$mv['merchname'];
		}
	}
	$return['merchs']=$merchs;
	$return['htmls']=$htmls;
	$shopset=$this->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'plugin_name'=>'jy_kongtiaoshop');
    if(!empty($_RQ['ordersn'])){
        $params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
        $return['params']['ordersn']=$_RQ['ordersn'];
    }
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['starttime'] || $_RQ['endtime']){
		$_RQ['starttime']=$_RQ['starttime']?strtotime($_RQ['starttime']):0;
		$_RQ['endtime']=$_RQ['endtime']?strtotime($_RQ['endtime']):time();
		$params['createtime between']=array($_RQ['starttime'],$_RQ['endtime']);
		$return['params']['createtime']=$time_range;
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_weishop_order',$params);
	if(count($order)>5000){
		exi('配置单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$ordergoods=array();
	foreach($order as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		$v['address']=json_decode($v['address'],true);
		if($v['address'] && ($v['address']['name'] || $v['address']['mobile'])){	
			$address=$v['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['addressid']));
		}
		$v['address']=model('core/member')->get_address_str($address);
		$v['address']=str_replace(',','，',$v['address']);
		$v['address']=str_replace("\n","，",$v['address']);
		$v['remark']=str_replace(',','，',$v['remark']);
		$v['remark']=str_replace("\n","，",$v['remark']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		if(is_array($v['diyformdata'])){
			foreach($v['diyformdata'] as $dk=>&$diyv){
					$v['data2'] .='【'.$diyv['title']."】：";
					if(is_array($diyv['data']) && $diyv['type']!=7){
						$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
					}
					if($diyv['type']==7){
						$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
					}
					$v['data2'] .=$diyv['data'];
			}
		}
		$invoice=pdo_get('jy_weishop_order_invoice',array('orderid'=>$v['id']));
		if($invoice){
			$type=$invoice['type']==2?'增值税专用发票':'电子普通发票';
			$v['invoice']='['.$type.'] 抬头：'.$invoice['name'].' ，税号：'.$invoice['code'].' ，开户行：'.$invoice['bank'].' ，银行账号：'.$invoice['bankcode'].' ，邮箱：'.$invoice['email'];
		}
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid','total'));
		foreach($goods as $k=>$gv){
			$info=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('api_id','api_pluginname','title'));
			if($info['api_pluginname']=='jy_supplier'){
                $info['supplier_id']=$info['api_id'];
            }
            $goodstitle=$info['title'];
			//空调规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs','goodssn','productsn'));
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$options=array();
				$selespename='无规格';
			}
			$num=$k+1;
			if($k){
				$v=array();
			}
			$v['goodstitle']='【空调'.$num.'】'.$goodstitle;
			$v['selespename']=$selespename;
			$v['goodssn']=$options['goodssn']?:$v['goodssn'];
			$v['productsn']=$options['productsn']?:$v['productsn'];
			$v['total']=$gv['total'];
			if($this->plugin_exists('jy_supplier') && $info['supplier_id']){
				$v['supplier']=pdo_getcolumn('jy_supplier_user',array('id'=>$info['supplier_id']),'merchname');
			}
            $v['costprice']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'costprice');
			$ordergoods[]=$v;
		}
	}
	$header=array(
		'ordersn'=>'配置单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'空调名称',
		'selespename'=>'规格',
		'goodssn'=>'编码',
		'productsn'=>'国家空调条码',
		'total'=>'数量',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'price'=>'金额',
		'status'=>'配置单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'paytime'=>'支付时间',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
		'invoice'=>'发票信息',
        'costprice'=>'成本价',
        'supplier'=>'供货商',
		'remark'=>'备注',
		'data2'=>'表单内容',
		'dispatchprice'=>'运费',
		'level_de_price'=>'会员折扣',
		'delcredit1'=>'积分抵扣',
		'enough_de_price'=>'满额立减',
		'coupon_de_price'=>'优惠券优惠',
		'luck_de_price'=>'运势优惠',
		'isdiscountprice'=>'促销优惠',
		'out_upfee'=>'打包费',
	);
	cfc('tools')->export2excel($header,$ordergoods,'商城配置单列表');
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id),array('ispay'));
	$print_type=18;
	$res=model('jy_weishop/printmsg')->storeprint('',$order_id,'','','','','','',$print_type,array('is18'=>1));
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该配置单所属门店没有添加打印任务！','error');
	}
}

if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'配置单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'配置单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'配置单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'store.order.detail','title'=>'配置单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'配置单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='refund'){
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_weishop_order',array('storeid'=>$_SESSION['storeid'],'id'=>$id));	
	}else{
		exi('请选择配置单！','error');
	}
	$params=array(
			'type'=>0,
			'orderid'=>intval($order['id']),
			'price'=>$order['price'],
			'content'=>'管理员后台手动替客户申请！',
	);
	model('jy_weishop/order/refund')->refund_create($params);
	exi('申请成功！请耐心等待财务审核！','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('storeid'=>$_SESSION['storeid'],'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('storeid'=>$_SESSION['storeid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){
	$this->check_candel('jy_weishop');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'storeid'=>$_SESSION['storeid'],
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_id'=>intval($_RQ['api_id']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
?>